package com.cqupt.scanning.system.file.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqupt.scanning.handler.exception.MyException;
import com.cqupt.scanning.security.utils.ResponseUtil;
import com.cqupt.scanning.system.acl.entity.AclUser;
import com.cqupt.scanning.system.acl.service.AclUserService;
import com.cqupt.scanning.system.admin.entity.*;
import com.cqupt.scanning.system.admin.service.*;
import com.cqupt.scanning.system.file.entity.bo.MesurementBO;
import com.cqupt.scanning.system.file.helper.*;
import com.cqupt.scanning.system.file.helper.utils.MultipartFileToFileUtil;
import com.cqupt.scanning.system.file.helper.utils.TimeFormatUtil;
import com.cqupt.scanning.system.file.helper.utils.UnZipUtil;
import com.cqupt.scanning.system.file.service.FileService;
import com.cqupt.scanning.system.utils.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;


import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipOutputStream;


/**
 * @Data: 2021/7/6 20:37
 * @Author: 宋宝梁
 */
@Service
@Slf4j
public class FileServiceImpl implements FileService {

    private FilePathConfig filePathConfig;
    private GenerateThreePDF generateThreePDF;
    private DepartmentService departmentService;
    private DeviceFootService deviceFootService;
    private DeviceThreeService deviceThreeService;
    private OrganizationService organizationService;
    private PatientFootService patientFootService;
    private PatientThreeService patientThreeService;
    private CopLeftFootService copLeftFootService;
    private CopRightFootService copRightFootService;
    private FeatureDataService featureDataService;
    private MesurementService mesurementService;
    private AclUserService aclUserService;

    @Autowired
    private ObjectFactory<GenerateFootPDF> objFactory;


    @Autowired
    public FileServiceImpl(FilePathConfig filePathConfig,
                           GenerateThreePDF generateThreePDF, DeviceFootService deviceFootService,
                           DepartmentService departmentService,OrganizationService organizationService,
                           PatientFootService patientFootService, PatientThreeService patientThreeService,
                           DeviceThreeService deviceThreeService, CopLeftFootService copLeftFootService,
                           CopRightFootService copRightFootService, FeatureDataService featureDataService,
                           MesurementService mesurementService, AclUserService aclUserService) {
        this.filePathConfig = filePathConfig;
        this.generateThreePDF = generateThreePDF;
        this.departmentService = departmentService;
        this.deviceFootService = deviceFootService;
        this.deviceThreeService = deviceThreeService;
        this.organizationService = organizationService;
        this.patientFootService = patientFootService;
        this.patientThreeService = patientThreeService;
        this.copLeftFootService = copLeftFootService;
        this.copRightFootService = copRightFootService;
        this.featureDataService = featureDataService;
        this.mesurementService = mesurementService;
        this.aclUserService = aclUserService;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void handleFootFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        String footFileRootDir = null;
        Map<String, String> savePathMap = null;
        try {
            if(file == null || file.isEmpty()) {
                ResponseUtil.out(response, R.error().message("请上传文件"));
                return;
            }

            footFileRootDir = filePathConfig.getSaveFootFilePath() + File.separator + TimeFormatUtil.getStrTime(TimeFormatUtil.ONE);
            File currentFootDir = new File(footFileRootDir);
            if (!currentFootDir.exists()) currentFootDir.mkdirs();
            File saveZipFile = MultipartFileToFileUtil.transferToFile(file, footFileRootDir);
            String saveZipFilePath = saveZipFile.getAbsolutePath(); // 压缩文件的保存位置
            File[] zipFile = currentFootDir.listFiles();
            if (zipFile == null || zipFile.length == 0) {
                ResponseUtil.out(response, R.error().message("请上传文件"));
                return;
            }

            UnZipUtil.doZip(zipFile[0], footFileRootDir); // 解压
            File[] files = currentFootDir.listFiles(); // 得到解压后的所有文件(含压缩文件)
            if (files == null || files.length == 0) {
                ResponseUtil.out(response, R.error().message("压缩文件为空"));
                return;
            }

            PatientFoot patientFoot = null;
            CopLeftFoot copLeftFoot = null;
            CopRightFoot copRightFoot = null;
            FeatureData featureData = null;
            List<String[]> csvListLeft = new ArrayList<>();
            List<String[]> csvListRight = new ArrayList<>();
            String leftFootCsvPath = null, rightFootCsvPath = null;
            for (File oneFile : files) {
                String oneFileName = oneFile.getName();
                if (oneFileName.contains("userinfo") && oneFileName.contains("json")) {
                    // 软件bug，解决该json文件重复数据
                    patientFoot = ReadUploadedFile.analysisJsonFile(oneFile, patientFoot, PatientFoot.class);
                    continue;
                }
                if (oneFileName.contains("left") && oneFileName.contains("json")) {
                    copLeftFoot = ReadUploadedFile.analysisJsonFile(oneFile, copLeftFoot, CopLeftFoot.class);
                    continue;
                }
                if (oneFileName.contains("right") && oneFileName.contains("json")) {
                    copRightFoot = ReadUploadedFile.analysisJsonFile(oneFile, copRightFoot, CopRightFoot.class);
                    continue;
                }
                if (oneFileName.contains("featureData") && oneFileName.contains("json")) {
                    featureData = ReadUploadedFile.analysisJsonFile(oneFile, featureData, FeatureData.class);
                    continue;
                }
                if (oneFileName.contains("leftFoot") && oneFileName.contains("csv")) {
                        leftFootCsvPath = oneFile.getAbsolutePath();
                        ReadUploadedFile.analysisCsvFile(oneFile, csvListLeft);
                    continue;
                }
                if (oneFileName.contains("rightFoot") && oneFileName.contains("csv")) {
                    rightFootCsvPath = oneFile.getAbsolutePath();
                    ReadUploadedFile.analysisCsvFile(oneFile, csvListRight);
                }
            }

            if (ObjectUtil.hasNull(patientFoot, copLeftFoot, copRightFoot, featureData, csvListLeft, csvListRight)) {
                ResponseUtil.out(response, R.error().message("文件数据异常"));
                return;
            }

            GenerateImage generateImage = new GenerateImage(filePathConfig.getTempImageDir());
            Map<String, Object> pictureData = new HashMap<>();
            generateImage.generateAllPictures(featureData, copLeftFoot, copRightFoot, csvListLeft, csvListRight, pictureData);

            GenerateFootPDF generateFootPDF = objFactory.getObject();
            savePathMap = generateFootPDF.execute(patientFoot, featureData, filePathConfig.getGaitTemplate(), filePathConfig.getBalanceTemplate(),
                    filePathConfig.getOutFileFootRootDirectory(), pictureData);

            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username).select("id"));
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().eq("user_id", user.getId()).select("dev_num"));

            DeviceFoot deviceFoot1 = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().eq("user_id", user.getId()).select("dep_id"));

            Department department = departmentService.getOne(new QueryWrapper<Department>().eq("id",deviceFoot1.getDepId()).select("org_num"));
            Organization organization = organizationService.getOne(new QueryWrapper<Organization>().eq("org_num",department.getOrgNum()).select("org_name"));


            patientFoot.setZipFilePath(saveZipFilePath)
                    .setLeftFootCsvPath(leftFootCsvPath)
                    .setRightFootCsvPath(rightFootCsvPath)
                    .setFileGaitPath(savePathMap.get("gaitPath"))
                    .setFileBalancePath(savePathMap.get("balancePath"))
                    .setDevNum(deviceFoot.getDevNum())
                    .setOrgName(organization.getOrgName());


            patientFootService.save(patientFoot);
            copLeftFoot.setPatientFootId(patientFoot.getId());
            copRightFoot.setPatientFootId(patientFoot.getId());
            featureData.setPatientFootId(patientFoot.getId());
            copLeftFootService.save(copLeftFoot);
            copRightFootService.save(copRightFoot);
            featureDataService.save(featureData);

            ResponseUtil.out(response, R.ok().data("id", patientFoot.getId()));
            log.info("足底扫描文件[{{}}]上传成功,生成报告[{}]、[{}]", file.getOriginalFilename(), generateFootPDF.getGaitPDFName(), generateFootPDF.getBlancePDFName());
        } catch (Exception e) {
            // 异常时删除生成的文件
            File footFile = new File(System.getProperty("user.dir") + File.separator + footFileRootDir);
            if (savePathMap != null) {
                if (savePathMap.containsKey("outFilePath") && savePathMap.get("outFilePath")!=null) {
                    File saveFile = new File(System.getProperty("user.dir") + File.separator + savePathMap.get("outFilePath"));
                    if (saveFile.exists()) FileHandler.deleteDir(saveFile);
                }
            }
            if (footFile.exists()) FileHandler.deleteDir(footFile);
            e.printStackTrace();
            throw new MyException(20001, "上传失败");
        } finally {
            // 删除创建的临时图片
            File imagesFile = new File(System.getProperty("user.dir") + File.separator + filePathConfig.getTempImageDir());
            if (imagesFile.exists()) {
                FileHandler.deleteDir(imagesFile);
            }
        }
    }

    @Override
    public void downloadGaitPdf(Integer id, HttpServletResponse response) {
        PatientFoot patientFoot = patientFootService.getOne(new QueryWrapper<PatientFoot>().eq("id", id).select("file_gait_path"));
        String filePath = patientFoot.getFileGaitPath();
        DownloadFileHelper.downloadOneFile(filePath, "足底步态分析报告[{{}}]下载成功", response);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void handleThreeFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String threeFileRootDir = null;
        String savePath = null;
        String uploadFileName = file.getOriginalFilename();
        try {
            if(file == null || file.isEmpty()) {
                ResponseUtil.out(response, R.error().message("请上传文件"));
                return;
            }
            threeFileRootDir = filePathConfig.getSaveThreeFilePath() + File.separator + TimeFormatUtil.getStrTime(TimeFormatUtil.ONE);
            File currentThreeDir = new File(threeFileRootDir);
            if (!currentThreeDir.exists()) currentThreeDir.mkdirs();
            File saveZipFile = MultipartFileToFileUtil.transferToFile(file, threeFileRootDir);
            String saveZipFilePath = saveZipFile.getAbsolutePath();
            File[] zipFile = currentThreeDir.listFiles();
            if (zipFile==null || zipFile.length==0) {
                ResponseUtil.out(response, R.error().message("请上传文件"));
                return;
            }

            UnZipUtil.doZip(zipFile[0], threeFileRootDir);
            File[] files = currentThreeDir.listFiles();
            if (files == null || files.length<1) {
                ResponseUtil.out(response, R.error().message("压缩文件为空"));
                return;
            }

            StringBuilder mesurementPath = new StringBuilder();
            StringBuilder picturesPath = new StringBuilder();
            for (File oneFile : files) {
                if (oneFile.isDirectory()) {
                    File[] manyFiles = new File(threeFileRootDir, oneFile.getName()).listFiles();
                    if (manyFiles == null || manyFiles.length<1) {
                        ResponseUtil.out(response, R.error().message("压缩文件为空"));
                        return;
                    }
                    for (File child : manyFiles) {
                        String childName = child.getName();
                        if (childName.contains("Mesurement") && childName.contains("csv")) {
                            mesurementPath.append(threeFileRootDir);
                            mesurementPath.append(File.separator);
                            mesurementPath.append(oneFile.getName());
                            mesurementPath.append(File.separator);
                            mesurementPath.append(childName);
                        }
                        if (childName.contains("Pictures")) {
                            picturesPath.append(threeFileRootDir);
                            picturesPath.append(File.separator);
                            picturesPath.append(oneFile.getName());
                            picturesPath.append(File.separator);
                            picturesPath.append(childName);
                        }
                    }
                }
            }

            if (StrUtil.hasEmpty(mesurementPath.toString())) {
                ResponseUtil.out(response, R.error().message("文件名不匹配"));
                return;
            }
            if (StrUtil.hasEmpty(picturesPath.toString())) {
                ResponseUtil.out(response, R.error().message("文件名不匹配"));
                return;
            }

            MesurementBO mesurementBO = ReadUploadedFile.analysisCsvFile(mesurementPath.toString());
            String baseInfo = mesurementBO.getBaseInfo();
            PatientThree patientThree = getPatientThree(baseInfo);
            savePath = generateThreePDF.execute(patientThree, mesurementBO, picturesPath.toString(), filePathConfig.getTemplatePDF3(), filePathConfig.getOutFileThreeRootDirectory());

            String username = SecurityContextHolder.getContext().getAuthentication().getName();
            AclUser user = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username).select("id"));
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().eq("user_id", user.getId()).select("dev_num"));

            patientThree.setDevNum(deviceThree.getDevNum());
            patientThree.setFilePath(savePath);
            patientThree.setZipFilePath(saveZipFilePath);
            patientThreeService.save(patientThree);

            Mesurement mesurement = new Mesurement();
            BeanUtils.copyProperties(mesurementBO, mesurement);
            mesurement.setPatientThreeId(patientThree.getId());
            mesurementService.save(mesurement);

            ResponseUtil.out(response, R.ok().data("id", patientThree.getId()));
            log.info("三维扫描文件[{{}}]上传成功,生成报告[{}]", file.getOriginalFilename(), generateThreePDF.getPdfName());
        } catch (Exception e) {
            File threeFile = new File(System.getProperty("user.dir") + File.separator + threeFileRootDir);
            File saveFile = new File(System.getProperty("user.dir") + File.separator + savePath);
            if (threeFile.exists()) FileHandler.deleteDir(threeFile);
            if (saveFile.exists()) FileHandler.deleteDir(saveFile);
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public void downloadThreeFile(Integer id, HttpServletResponse response) {
        PatientThree patientThree = patientThreeService.getOne(new QueryWrapper<PatientThree>().eq("id", id).select("file_path"));
        String filePath = patientThree.getFilePath();
        DownloadFileHelper.downloadOneFile(filePath, "三维扫描报告[{}]下载成功", response);
    }

    @Override
    public void downloadFootZipFile(Integer id, HttpServletResponse response) {
        PatientFoot patientFoot = patientFootService.getOne(new QueryWrapper<PatientFoot>().eq("id", id).select("zip_file_path"));
        String zipFilePath = patientFoot.getZipFilePath();
        DownloadFileHelper.downloadOneFile(zipFilePath, "足底扫描压缩文件[{}]下载成功", response);
    }

    @Override
    public void downloadThreeZipFile(Integer id, HttpServletResponse response) {
        PatientThree patientThree = patientThreeService.getOne(new QueryWrapper<PatientThree>().eq("id", id).select("zip_file_path"));
        String zipFilePath = patientThree.getZipFilePath();
        DownloadFileHelper.downloadOneFile(zipFilePath, "三维扫描压缩文件[{}]下载成功", response);
    }

    @Override
    public void downloadLeftCsvFile(Integer id, HttpServletResponse response) {
        PatientFoot patientFoot = patientFootService.getOne(new QueryWrapper<PatientFoot>().eq("id", id).select("left_foot_csv_path"));
        String leftFootCsvPath = patientFoot.getLeftFootCsvPath();
        DownloadFileHelper.downloadOneFile(leftFootCsvPath, "足底扫描左脚csv文件[{}]下载成功", response);
    }

    @Override
    public void downloadRightCsvFile(Integer id, HttpServletResponse response) {
        PatientFoot patientFoot = patientFootService.getOne(new QueryWrapper<PatientFoot>().eq("id", id).select("right_foot_csv_path"));
        String rightFootCsvPath = patientFoot.getRightFootCsvPath();
        DownloadFileHelper.downloadOneFile(rightFootCsvPath, "足底扫描右脚csv文件[{}]下载成功", response);

    }

    @Override
    public void downloadBalancePdf(Integer id, HttpServletResponse response) {
        PatientFoot patientFoot = patientFootService.getOne(new QueryWrapper<PatientFoot>().eq("id", id).select("file_balance_path"));
        String zipFilePath = patientFoot.getFileBalancePath();
        DownloadFileHelper.downloadOneFile(zipFilePath, "足底平衡分析报告[{}]下载成功", response);
    }

    @Override
    public R downloadBatchFootZipFile(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
        Map<String, List<String>> map = new HashMap<>();
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            QueryWrapper<PatientFoot> patientFoot = new QueryWrapper<PatientFoot>().like("dev_num",devNum).ge("create_time", startTime).le("create_time", endTime);
            List<String> FileGaitPath=(patientFootService.list(patientFoot)).stream().map(e -> e.getFileGaitPath()).collect(Collectors.toList());
            map.put(aclUser.getUsername(),FileGaitPath);
            byte[] zipfile = DownloadFileHelper.zipFile(map,"足底报告",response);
            DownloadFileHelper.downloadBatchFiles(zipfile,request,response);
        }
        return R.error();
    }

    @Override
    public R downloadBatchBalanceZipFile(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
        Map<String, List<String>> map = new HashMap<>();
        if (aclUser.getType().equals("步态分析")) {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceFoot.getDevNum();
            QueryWrapper<PatientFoot> patientFoot = new QueryWrapper<PatientFoot>().like("dev_num",devNum).ge("create_time", startTime).le("create_time", endTime);
            List<String> FileBalanceFilePath=(patientFootService.list(patientFoot)).stream().map(e -> e.getFileBalancePath()).collect(Collectors.toList());
            map.put(aclUser.getUsername(),FileBalanceFilePath);
            byte[] zipfile = DownloadFileHelper.zipFile(map,"平衡报告",response);
            DownloadFileHelper.downloadBatchFiles(zipfile,request,response);
    }
        return R.error();
    }

    @Override
    public R downloadBatchThreeZipFile(String startTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        AclUser aclUser = aclUserService.getOne(new QueryWrapper<AclUser>().eq("username", username));
        Map<String, List<String>> map = new HashMap<>();
        if (aclUser.getType().equals("三维扫描")) {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.getId()));
            String devNum = deviceThree.getDevNum();
            QueryWrapper<PatientThree> patientThree = new QueryWrapper<PatientThree>().like("dev_num",devNum).ge("create_time", startTime).le("create_time", endTime);
            List<String> FileThreePath=(patientThreeService.list(patientThree)).stream().map(e -> e.getFilePath()).collect(Collectors.toList());
            map.put(aclUser.getUsername(),FileThreePath);
            byte[] zipfile = DownloadFileHelper.zipFile(map,"三维扫描报告",response);
            DownloadFileHelper.downloadBatchFiles(zipfile,request,response);
        }
        return R.error();
    }

    @Override
    public R downloadBatchOriginalFootZipFile(String beginTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<AclUser> aclUser = aclUserService.list(new QueryWrapper<AclUser>().eq("note", 0).like("type","步态分析"));
        List<String> user = aclUser.stream().map(AclUser::getUsername).collect(Collectors.toList());
        Map<String, List<String>> map = new HashMap<>();
        for (int i=0;i<aclUser.size();i++)
        {
            DeviceFoot deviceFoot = deviceFootService.getOne(new QueryWrapper<DeviceFoot>().select("dev_num").eq("user_id", aclUser.get(i).getId()));
            String devNum = deviceFoot.getDevNum();
            QueryWrapper<PatientFoot> patientFoot = new QueryWrapper<PatientFoot>().like("dev_num",devNum).ge("create_time", beginTime).le("create_time", endTime);
            List<String> ZipFilePath=(patientFootService.list(patientFoot)).stream().map(e -> e.getZipFilePath()).collect(Collectors.toList());
            map.put(user.get(i),ZipFilePath);
        }
        byte[] zipfile=DownloadFileHelper.zipFile(map,"步态分析源文件压缩包",response);
        DownloadFileHelper.downloadBatchFiles(zipfile,request,response);
        return R.ok();
    }

    @Override
    public R downloadBatchOriginalThreeZipFile(String beginTime, String endTime, HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<AclUser> aclUser = aclUserService.list(new QueryWrapper<AclUser>().eq("note", 0).like("type","三维扫描"));
        List<String> user = aclUser.stream().map(AclUser::getUsername).collect(Collectors.toList());
        Map<String, List<String>> map = new HashMap<>();
        for (int i=0;i<aclUser.size();i++)
        {
            DeviceThree deviceThree = deviceThreeService.getOne(new QueryWrapper<DeviceThree>().select("dev_num").eq("user_id", aclUser.get(i).getId()));
            String devNum = deviceThree.getDevNum();
            QueryWrapper<PatientThree> patientThree = new QueryWrapper<PatientThree>().like("dev_num",devNum).ge("create_time", beginTime).le("create_time", endTime);
            List<String> ZipFilePath=(patientThreeService.list(patientThree)).stream().map(e -> e.getZipFilePath()).collect(Collectors.toList());
            map.put(user.get(i),ZipFilePath);
        }
        byte[] zipfile=DownloadFileHelper.zipFile(map,"三维扫描源文件压缩包",response);
        DownloadFileHelper.downloadBatchFiles(zipfile,request,response);
        return R.error();
    }

    /**
     * 解析三维扫描病人的基本信息
     * @param baseInfo
     * @return
     */
    private PatientThree getPatientThree(String baseInfo) {
        String reg = "[^\u4e00-\u9fa5]";
        String time = null, date = null, sex = null, devNum = null;
        String[] arr = baseInfo.split(" ");
        PatientThree patientThree = new PatientThree();
        for (int i=arr.length-1; i>=0; i--) {
            if (i == arr.length-1) {
                time = arr[arr.length-1];
                continue;
            }
            if (i == arr.length-2) {
                date = arr[arr.length-2];
                continue;
            }
            if (i == arr.length-3) {
                String str = arr[arr.length-3];
                String[] s = str.split("_");
                devNum = s[0];
                continue;
            }
            if (i == arr.length-4) {
                sex = arr[arr.length-4];
                if (sex.contains("Female")) {
                    sex = "女";
                } else {
                    sex = "男";
                }
                break;
            }
        }
        String testTime = date + " " + time;
        String name = baseInfo.replaceAll(reg, "");
        patientThree.setName(name);
        patientThree.setSex(sex);
//        patientThree.setDevNum(devNum);
        patientThree.setTestTime(testTime);
        return patientThree;
    }
}
